package com.sxx.mall.ware.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxx.commons.dto.SkuHasStockDTO;
import com.sxx.commons.exception.BizCodeEnum;
import com.sxx.commons.exception.NoStockException;
import com.sxx.commons.utils.PageUtils;
import com.sxx.commons.utils.R;
import com.sxx.mall.ware.entity.WareSkuEntity;
import com.sxx.mall.ware.service.WareSkuService;
import com.sxx.mall.ware.vo.LockStockResult;
import com.sxx.mall.ware.vo.WareSkuLockVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品库存
 *
 * @author sxx
 * @email 1289139305@qq.com
 * @date 2024-07-10 15:52:05
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;


    @PostMapping("lock/order")
    public R orderLockStock(@RequestBody WareSkuLockVO vo){
        try {
            Boolean flag = wareSkuService.orderLockStock(vo);
        }catch (NoStockException e){
            return R.error(BizCodeEnum.NO_STOCK_EXCEPTION.getCode(), BizCodeEnum.NO_STOCK_EXCEPTION.getMsg());
        }

        return R.ok();
    }

    /**
     * 列表ware/waresku/list?&page=1&limit=10&skuId=&wareId=2
     */
    @RequestMapping("/list")
    //@RequiresPermissions("ware:waresku:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 是否有库存
     */
    @RequestMapping("/hasStock")
    public List<SkuHasStockDTO> skuHasStock(@RequestBody List<Long> skuIds){
        return wareSkuService.hasStock(skuIds);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:waresku:info")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:waresku:delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
